//
//  UserViewController.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 light. All rights reserved.
//

#import "UserViewController.h"
#import <AFNetworking.h>
#import "User_infoModel.h"
#import <UIImageView+WebCache.h>
#import "CityPageModel.h"
#import "UserProductTableViewCell.h"
#import "Use_tripsModel.h"
#import "UserTripsTableViewCell.h"
@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *userTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *secondArray;

@end

@implementation UserViewController

- (void)viewWillAppear:(BOOL)animated
{
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.secondArray = [NSMutableArray array];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:192 / 255.0 blue:203 / 255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
   self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Cancel2"] imageWithRenderingMode:1]style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    self.userTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.userTableView.backgroundColor = [UIColor colorWithRed:251 / 255.0  green:248 / 255.0 blue:241 / 255.0 alpha:1];
    
   
    [self.view addSubview:self.userTableView];
    self.userTableView.delegate = self;
    self.userTableView.dataSource = self;
    [self.userTableView registerClass:[UserProductTableViewCell class] forCellReuseIdentifier:@"reuse"
     ];
    
    [self.userTableView registerClass:[UserTripsTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.userTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://api.breadtrip.com/v3/user/%@",self.myId] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = [responseObject objectForKey:@"data"];
        NSDictionary *infoDic = [data objectForKey:@"user_info"];
        User_infoModel *userModel = [[User_infoModel alloc] init];
        [userModel setValuesForKeysWithDictionary:infoDic];
        NSDictionary *productsDic = [data objectForKey:@"products"];
        NSMutableArray *productArray = [productsDic objectForKey:@"data"];
        for (NSDictionary *dic in productArray) {
            CityPageModel *model = [[CityPageModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            //NSLog(@"%@",model.title);
            
        }
        NSDictionary *tripsDic = [data objectForKey:@"trips"];
        NSMutableArray *tripsArray = [tripsDic objectForKey:@"data"];
        for (NSDictionary *dic in tripsArray) {
            Use_tripsModel *model = [[Use_tripsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.secondArray addObject:model];
           
        }
        
        
        self.navigationItem.title = userModel.name;
        UIView *bigView = [[UIView alloc] init];
        bigView.backgroundColor = [UIColor colorWithRed:251 / 255.0  green:248 / 255.0 blue:241 / 255.0 alpha:1];
       // self.userTableView.tableHeaderView = bigView;
        
        
        //计算文字高度
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
        //这里的宽度一定要和我们显示文字的label的宽度一样;
        CGRect rect = [userModel.user_desc boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 60, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 400, [UIScreen mainScreen].bounds.size.width - 60, rect.size.height)];
        textLabel.numberOfLines = 0;
        textLabel.text = userModel.user_desc;
        textLabel.textColor = [UIColor grayColor];
        textLabel.textAlignment = NSTextAlignmentCenter;
        [bigView addSubview:textLabel];
        UIImageView *smallImage = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 30, 330, 60, 60)];
        smallImage.image = [UIImage imageNamed:@"keaixiaotubiao"];
        [bigView addSubview:smallImage];
        bigView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, rect.size.height + 430);
        
        
        
        UIImageView *coverImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
        [coverImageV sd_setImageWithURL:[NSURL URLWithString:userModel.cover]];
        [bigView addSubview:coverImageV];
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
        backView.backgroundColor = [UIColor blackColor];
        backView.alpha = 0.3;
        [bigView addSubview:backView];
        
        UIImageView *userImageV = [[UIImageView alloc] initWithFrame:CGRectMake(( [UIScreen mainScreen].bounds.size.width - 80) / 2, 60, 80, 80)];
        userImageV.layer.masksToBounds = YES;
        userImageV.layer.cornerRadius = 40;
        
        [userImageV sd_setImageWithURL:[NSURL URLWithString:userModel.avatar_m]];
        
        [bigView addSubview:userImageV];
        
        UILabel *fenLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 100, 100 , 40, 30)];
        fenLabel.textColor = [UIColor whiteColor];
        fenLabel.text = @"粉丝";
        [bigView addSubview:fenLabel];
        
        UILabel *guanLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 + 60, 100, 40, 30)];
        guanLabel.textColor = [UIColor whiteColor];
        guanLabel.text = @"关注";
        [bigView addSubview:guanLabel];
        
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 100, 70, 50, 40)];
        leftLabel.textColor = [UIColor whiteColor];
        leftLabel.font = [UIFont systemFontOfSize:20];
        leftLabel.font = [UIFont boldSystemFontOfSize:18];
        leftLabel.text = [NSString stringWithFormat:@"%@",userModel.followers_count];
        [bigView addSubview:leftLabel];
        
        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 + 65, 70, 50, 40)];
        rightLabel.textColor = [UIColor whiteColor];
        rightLabel.font = [UIFont systemFontOfSize:20];
        rightLabel.font = [UIFont boldSystemFontOfSize:18];
        rightLabel.text = [NSString stringWithFormat:@"%@",userModel.followings_count];
        [bigView addSubview:rightLabel];
        
        
        
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, [UIScreen mainScreen].bounds.size.width - 40, 50)];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.font = [UIFont systemFontOfSize:20];
        nameLabel.font = [UIFont boldSystemFontOfSize:18];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.text = userModel.name;
        [bigView addSubview:nameLabel];
        
        UILabel *professionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, [UIScreen mainScreen].bounds.size.width - 40, 50)];
        professionLabel.textColor = [UIColor whiteColor];
        professionLabel.textAlignment = NSTextAlignmentCenter;
        professionLabel.text = userModel.profession;
        [bigView addSubview:professionLabel];
        
        UIButton *guanButton = [UIButton buttonWithType:UIButtonTypeCustom];
        guanButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 100, 230, 90, 35);
        guanButton.backgroundColor = [UIColor colorWithRed:251 / 255.0  green:248 / 255.0 blue:241 / 255.0 alpha:1];
        [guanButton setTitle:@"关注" forState:UIControlStateNormal];
        [guanButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        guanButton.layer.masksToBounds = YES;
        guanButton.layer.cornerRadius = 15;
        [bigView addSubview:guanButton];
        
        UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        sendButton.backgroundColor = [UIColor colorWithRed:251 / 255.0  green:248 / 255.0 blue:241 / 255.0 alpha:1];
         [sendButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        sendButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 + 10, 230, 90, 35);
        [sendButton setTitle:@"发私信" forState:UIControlStateNormal];
        sendButton.layer.masksToBounds = YES;
        sendButton.layer.cornerRadius = 15;
        [bigView addSubview:sendButton];
        
        self.userTableView.tableHeaderView = bigView;
        [self.userTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
}

- (void)leftAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}
//取消section的吸附效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.userTableView)
    {
        CGFloat sectionHeaderHeight = 60; //sectionHeaderHeight
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"猎人游记";
    }
    return @"游记&故事集";
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.dataArray.count;
    }
    return self.secondArray.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UserProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        CityPageModel *model = [self.dataArray objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
  
    
    UserTripsTableViewCell *tripsCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    Use_tripsModel *model = [self.secondArray objectAtIndex:indexPath.row];
    tripsCell.selectionStyle = UITableViewCellSelectionStyleNone;
    tripsCell.model = model;
    return tripsCell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
     return self.view.frame.size.height / 5 * 3;
    }
     return self.view.frame.size.height / 4;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
